<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="父id" prop="pid">-->
<!--        <el-input-->
<!--          v-model="queryParams.pid"-->
<!--          placeholder="请输入父id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="车次名称" prop="trainNumberName">
        <el-input
          v-model="queryParams.trainNumberName"
          placeholder="请输入车次名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="火车名称" prop="licensePlateNumber">
        <el-input
          v-model="queryParams.licensePlateNumber"
          placeholder="请输入火车名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发车日期" prop="departureDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.departureDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择发车日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="起始站" prop="startingStation">
        <el-input
          v-model="queryParams.startingStation"
          placeholder="请输入起始站"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终点站" prop="terminus">
        <el-input
          v-model="queryParams.terminus"
          placeholder="请输入终点站"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发车时间" prop="departureTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.departureTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择发车时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="到站时间" prop="arrivalTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.arrivalTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择到站时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="座位类型" prop="seatType">
        <el-select v-model="queryParams.seatType" placeholder="请选择座位类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.train_seat"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="总时长" prop="totalDuration">
        <el-input
          v-model="queryParams.totalDuration"
          placeholder="请输入总时长"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-model="queryParams.remarks"
          placeholder="请输入备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注1" prop="remarks1">
        <el-input
          v-model="queryParams.remarks1"
          placeholder="请输入备注1"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:information:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:information:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:information:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:information:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="informationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="tid" />
      <el-table-column label="车次名称" align="center" prop="trainNumberName" />
      <el-table-column label="火车名称" align="center" prop="licensePlateNumber" />
      <el-table-column label="图片" align="center" prop="picture" >
        <template slot-scope="scope">
            <img :src="scope.row.picture" style="width: 100px;height: 100px">
        </template>
      </el-table-column>
      <el-table-column label="发车日期" align="center" prop="departureDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.departureDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="起始站" align="center" prop="startingStation" />
      <el-table-column label="终点站" align="center" prop="terminus" />
      <el-table-column label="发车时间" align="center" prop="departureTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.departureTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到站时间" align="center" prop="arrivalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrivalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="座位类型" align="center" prop="seatType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.train_seat" :value="scope.row.seatType"/>
        </template>
      </el-table-column>
      <el-table-column label="总时长" align="center" prop="totalDuration" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="备注1" align="center" prop="remarks1" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:information:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:information:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车次信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车次名称" prop="trainNumberName">
          <el-input v-model="form.trainNumberName" placeholder="请输入车次名称" />
        </el-form-item>
        <el-form-item label="火车名称" prop="licensePlateNumber">
          <el-select v-model="form.licensePlateNumber" placeholder="请输入车次名称"  @change="listProjectCentrall" >
            <el-option
              v-for="item in datatt"
              :key="item.value"
              :label="item.label"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片">
          <imageUpload v-model="form.picture"/>
        </el-form-item>
        <el-form-item label="发车日期" prop="departureDate">
          <el-date-picker clearable size="small"
            v-model="form.departureDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发车日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="起始站" prop="startingStation">
          <el-input v-model="form.startingStation" placeholder="请输入起始站" />
        </el-form-item>
        <el-form-item label="终点站" prop="terminus">
          <el-input v-model="form.terminus" placeholder="请输入终点站" />
        </el-form-item>
        <el-form-item label="发车时间" prop="departureTime">
          <el-date-picker clearable size="small"
            v-model="form.departureTime"
            type="datetime"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="选择发车时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="到站时间" prop="arrivalTime">
          <el-date-picker clearable size="small"
            v-model="form.arrivalTime"
            type="datetime"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="选择到站时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="座位类型" prop="seatType">
          <el-select v-model="form.seatType" placeholder="请选择座位类型">
            <el-option
              v-for="dict in dict.type.train_seat"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="总时长" prop="totalDuration">
          <el-input v-model="form.totalDuration" placeholder="请输入总时长" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="备注1" prop="remarks1">
          <el-input v-model="form.remarks1" placeholder="请输入备注1" />
        </el-form-item>
        <el-divider content-position="center">${subTable.functionName}信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTicketPrice">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTicketPrice">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="ticketPriceList" :row-class-name="rowTicketPriceIndex" @selection-change="handleTicketPriceSelectionChange" ref="ticketPrice">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
<!--          <el-table-column label="列车名" prop="trainName">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.trainName" placeholder="请输入列车名" />-->
<!--            </template>-->
<!--          </el-table-column>-->

          <el-table-column label="车票种类" prop="ticketType">
            <template slot-scope="scope">
              <el-input v-model="scope.row.ticketType" placeholder="请输入车票种类" />
            </template>
          </el-table-column>
          <el-table-column label="车票数量" prop="numberOfTickets">
            <template slot-scope="scope">
              <el-input v-model="scope.row.numberOfTickets" placeholder="请输入车票数量" />
            </template>
          </el-table-column>
          <el-table-column label="车票价格" prop="ticketPrice">
            <template slot-scope="scope">
              <el-input v-model="scope.row.ticketPrice" placeholder="请输入车票价格" />
            </template>
          </el-table-column>
<!--          <el-table-column label="备用字段" prop="remakers">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.remakers" placeholder="请输入备用字段" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="备用字段1" prop="remakers1">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.remakers1" placeholder="请输入备用字段1" />-->
<!--            </template>-->
<!--          </el-table-column>-->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInformation, getInformation, delInformation, addInformation, updateInformation, exportInformation } from "@/api/system/information";
import {listIn} from "@/api/system/in"
export default {
  name: "Information",
  dicts: ['train_seat'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTicketPrice: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车次信息表格数据
      informationList: [],
      // ${subTable.functionName}表格数据
      ticketPriceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      datatt:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pid: null,
        trainNumberName: null,
        licensePlateNumber: null,
        picture: null,
        departureDate: null,
        startingStation: null,
        terminus: null,
        departureTime: null,
        arrivalTime: null,
        seatType: null,
        totalDuration: null,
        remarks: null,
        remarks1: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.listIn1();
  },
  methods: {
    /** 查询车次信息列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams,"6666");
      listInformation(this.queryParams).then(response => {
        this.informationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tid: null,
        pid: null,
        trainNumberName: null,
        licensePlateNumber: null,
        picture: null,
        departureDate: null,
        startingStation: null,
        terminus: null,
        departureTime: null,
        arrivalTime: null,
        seatType: null,
        totalDuration: null,
        remarks: null,
        remarks1: null
      };
      this.ticketPriceList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车次信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tid = row.tid || this.ids
      getInformation(tid).then(response => {
        this.form = response.data;
        this.ticketPriceList = response.data.ticketPriceList;
        this.open = true;
        this.title = "修改车次信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.ticketPriceList = this.ticketPriceList;
          if (this.form.tid != null) {
            updateInformation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.pid=this.queryParams.pid;
            this.scope.row.trainName=this.form.trainNumberName
            this.form.picture="http://127.0.0.1:8080"+this.form.picture;
            addInformation(this.form).then(response => {
              console.log(response)
              this.$modal.msgSuccess("新增成功");
              this.open = false;
                this.queryParams={};
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tids = row.tid || this.ids;
      this.$modal.confirm('是否确认删除车次信息编号为"' + tids + '"的数据项？').then(function() {
        return delInformation(tids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** ${subTable.functionName}序号 */
    rowTicketPriceIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** ${subTable.functionName}添加按钮操作 */
    handleAddTicketPrice() {
      let obj = {};
      obj.trainName = "";
      obj.ticketType = "";
      obj.numberOfTickets = "";
      obj.ticketPrice = "";
      obj.remakers = "";
      obj.remakers1 = "";
      this.ticketPriceList.push(obj);
    },
    /** ${subTable.functionName}删除按钮操作 */
    handleDeleteTicketPrice() {
      if (this.checkedTicketPrice.length == 0) {
        this.$modal.msgError("请先选择要删除的${subTable.functionName}数据");
      } else {
        const ticketPriceList = this.ticketPriceList;
        const checkedTicketPrice = this.checkedTicketPrice;
        this.ticketPriceList = ticketPriceList.filter(function(item) {
          return checkedTicketPrice.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTicketPriceSelectionChange(selection) {
      this.checkedTicketPrice = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有车次信息数据项？').then(() => {
        this.exportLoading = true;
        return exportInformation(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    //列车查询
    listIn1(){
      listIn(this.queryParams).then(response =>{
        for (let i = 0; i <response.rows.length ; i++) {
          this.datatt.push(response.rows[i].licensePlateNumber);
        }
        console.log("this.datatt",this.datatt)
      })
    },
    listProjectCentrall(){
      var that=this;
      if (null != this.form.licensePlateNumber && '' != this.form.licensePlateNumber) {
        this.queryParams.licensePlateNumber =this.form.licensePlateNumber
        console.log(that.queryParams.pid)
        listIn(this.queryParams).then(response =>{
          for (let i = 0; i <response.rows.length ; i++) {
            this.queryParams.pid=response.rows[0].pid;
            console.log(this.queryParams.pid,"5555")
          }
        })
        console.log(this.queryParams.pid)
      }
    }
  }
};
</script>
